package com.example.testhiber.initPackage.business.controller;


import com.example.testhiber.initPackage.business.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("picture")
public class FolderController {
    private static final Logger logger = LoggerFactory.getLogger(FolderController.class);
    private static final String PATH="/picture/";

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/folderList")
    public Result folderList(){
        String sql = " select * from t_folder where deleted=0";
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);

        return new Result(true,"操作成功",list);
    }

    /**
     * 读取某个文件夹下面所有的文件目录
     * @return
     */
    @RequestMapping("/readPictures")
    public ModelAndView readPictures(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName(PATH+"picture");
        return mav;
    }



    /**
     * 打开文件目录
     * @return
     */
    @RequestMapping("/initPicture")
    public void initPicture(){

        try {
            System.out.println(Desktop.isDesktopSupported());
            System.setProperty("java.awt.headless", "false");
            // 先判断当前平台是否支持桌面
            if (Desktop.isDesktopSupported()) {
                // 获取当前平台桌面实例
                Desktop desktop = Desktop.getDesktop();

                // 使用默认浏览器打开链接
                //            desktop.browse(URI.create("https://blog.csdn.net/xietansheng"));

                // 打开指定文件/文件夹
//                desktop.open(new File("C:\\"));

                Desktop.getDesktop().open(new File("D:"+ File.separator +"resource"));

            } else {
                System.out.println("当前平台不支持 Desktop");
            }
        }catch (Exception e){
        }

    }

    public static void main(String[] args) {
        try {
            Desktop.getDesktop().open(new File("D:"+ File.separator +"resource"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void openFile(){
        try {
            Desktop.getDesktop().open(new File("D:"+ File.separator +"resource"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

